/**
 * 	The MIT License (MIT)

	Copyright (c) 2016-2016 d3leaf@126.com

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
 */
package cc.ecore.spring.jfinal.plugin;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.util.Assert;

public class SpringKit {

	/*---------------- FileSystemXmlApplicationContext -------------*/
	/**
	 * @Title: 创建 ApplicationContext
	 * @param configurations
	 * @return FileSystemXmlApplicationContext 
	 */
	public static FileSystemXmlApplicationContext newFileSystemXmlApplicationContextByConfigurations(String... configurations) {
		Assert.notNull(configurations);
		return new FileSystemXmlApplicationContext(configurations);
	}

	/*---------------- AnnotationConfigApplicationContext -------------*/
	/**
	 * @Title: 创建 ApplicationContext
	 * @param basePackages
	 * @return AnnotationConfigApplicationContext 
	 */
	public static AnnotationConfigApplicationContext newAnnotationConfigApplicationContextByBasePackages(String... basePackages) {
		Assert.notNull(basePackages);
		return new AnnotationConfigApplicationContext(basePackages);
	}

	/**
	 * @Title: 创建 ApplicationContext
	 * @param annotatedClasses
	 * @return AnnotationConfigApplicationContext 
	 */
	public static AnnotationConfigApplicationContext newAnnotationConfigApplicationContextByAnnotatedClasses(
			Class<?>... annotatedClasses) {
		Assert.notNull(annotatedClasses);
		return new AnnotationConfigApplicationContext(annotatedClasses);
	}

	/**
	 * @Title: 创建 ApplicationContext
	 * @param annotatedClasses
	 * @return AnnotationConfigApplicationContext 
	 */
	public static AnnotationConfigApplicationContext newAnnotationConfigApplicationContextByAnnotatedClasses(
			String... annotatedClasses) {
		return newAnnotationConfigApplicationContextByAnnotatedClasses(forNames(annotatedClasses));
	}

	/*---------------- ClassPathXmlApplicationContext -------------*/
	/**
	 * @Title: 创建 ApplicationContext  
	 * @param configLocations
	 * @return ApplicationContext 
	 */
	public static ClassPathXmlApplicationContext newClassPathXmlApplicationContextByConfigLocations(String... configLocations) {
		Assert.notNull(configLocations);
		return new ClassPathXmlApplicationContext(configLocations);
	}

	/**
	 * @Title: 生产 bean builder
	 * @param serviceClass
	 * @return
	 */
	public static BeanDefinitionBuilder genericBean(Class<?> serviceClass) {
		Assert.notNull(serviceClass);
		return BeanDefinitionBuilder.genericBeanDefinition(serviceClass);
	}

	/**
	 * @Title: 生产 bean builder
	 * @param beanClassName
	 * @return
	 */
	public static BeanDefinitionBuilder genericBean(String beanClassName) {
		Assert.notNull(beanClassName);
		return BeanDefinitionBuilder.genericBeanDefinition(beanClassName);
	}

	/**
	 * 向spring容器注册bean
	 * @Title: 
	 * @param beanName
	 * @param beanDefinition
	 * @param context
	 */
	public static void registerBean(ApplicationContext context, String beanName, BeanDefinition beanDefinition) {
		// ApplicationContext 不能为空
		Assert.notNull(context);
		// 断言 bean 没有定义
		Assert.state(!context.containsBean(beanName), "bean [" + beanName + "] is exist.");
		// BeanDefinition 不能为空
		Assert.notNull(beanDefinition);
		// 注册
		ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) context;
		BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) configurableApplicationContext.getBeanFactory();
		beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
	}

	// --------------------------------------------------------
	// private function
	// --------------------------------------------------------

	static Class<?>[] forNames(String... classNames) {
		int length = 0;
		if (classNames != null) length = classNames.length;
		Class<?>[] classes = new Class[length];
		for (int i = 0; i < length; i++) {
			classes[i] = forName(classNames[i]);
		}
		return classes;
	}

	@SuppressWarnings("unchecked")
	static <T> Class<T> forName(String className) {
		try {
			return (Class<T>) Class.forName(className);
		} catch (ClassNotFoundException e) {
			throw new IllegalArgumentException(e);
		}
	}
}
